<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" size="small" style="width: 120px">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="年份" prop="activeYear">
        <el-date-picker
          v-model="queryParams.activeYear"
          type="year"
          value-format="yyyy"
          placeholder="选择年份"
          style="width: 120px">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="名称" prop="dimensionName">
        <el-input
          v-model="queryParams.dimensionName"
          placeholder="请输入维度名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="编码" prop="dimensionCode">
        <el-input
          v-model="queryParams.dimensionCode"
          placeholder="请输入维度编码"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAddBtn"
                   v-hasPermi="['quota:dimension:add']">新增
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="dimensionList"
      row-key="id"
      lazy
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      @selection-change="handleSelectionChange">
      <el-table-column label="维度名称" prop="dimensionName" show-overflow-tooltip />
      <el-table-column label="维度编码" align="center" prop="dimensionCode"/>
      <el-table-column label="维度类型" align="center" prop="dimensionType" :formatter="dimensionTypeFormat" width="80px"/>
      <el-table-column label="维度属性" align="center" prop="dimensionAttr" :formatter="dimensionAttrFormat" width="80px"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" width="80px"/>
      <el-table-column label="使用年份" align="center" prop="activeYear" width="80px"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['quota:dimension:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['quota:dimension:add']"
          >新增
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['quota:dimension:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

<!--    <pagination-->
<!--      v-show="total>0"-->
<!--      :total="total"-->
<!--      :page.sync="queryParams.current"-->
<!--      :limit.sync="queryParams.size"-->
<!--      @pagination="getList"-->
<!--    />-->

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="维度名称" prop="dimensionName">
              <el-input v-model="form.dimensionName" placeholder="请输入维度名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="维度编码" prop="dimensionCode">
              <el-input v-model="form.dimensionCode"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="使用年份">
              <el-date-picker
                v-model="form.activeYear"
                type="year"
                value-format="yyyy"
                placeholder="选择年份">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="维度类型">
              <el-select v-model="form.dimensionType" placeholder="请选择维度类型">
                <el-option
                  v-for="dict in dimensionTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="维度属性">
              <el-select v-model="form.dimensionAttr" placeholder="请选择维度属性">
                <el-option
                  v-for="dict in dimensionAttrOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="维度说明">
            <el-input type="textarea" v-model="form.comments"></el-input>
          </el-form-item>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listDimension,
    getDimension,
    delDimension,
    addDimension,
    updateDimension,
    exportDimension
  } from '@/api/metadata/dimension'
  import Cookies from 'js-cookie'

  export default {
    name: 'dimension',
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 维度表格数据
        dimensionList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层    00000008
        open: false,
        // 当前年份
        // 维度类型：1,公用;2,私有
        dimensionTypeOptions: [{ dictValue: '1', dictLabel: '公用' }, { dictValue: '2', dictLabel: '私有' }],
        // 维度属性：1,动态;2.静态
        dimensionAttrOptions: [{ dictValue: '1', dictLabel: '动态' }, { dictValue: '2', dictLabel: '静态' }],
        // 状态：0删除 1启用 2禁用
        statusOptions: [{ dictValue: '1', dictLabel: '启用' }, { dictValue: '2', dictLabel: '禁用' }],
        // 查询参数
        queryParams: {
          current: 1,
          size: 10000,
          id: undefined,
          dimensionName: undefined,
          dimensionCode: undefined,
          upperId: undefined,
          dimensionType: undefined,
          dimensionAttr: undefined,
          status: undefined,
          activeYear: undefined,
          likes: undefined
        },
        // 表单参数
        form: {

        },
        // 表单校验
        rules: {
          dimensionName: [
            { required: true, message: '维度名称不能为空', trigger: 'blur' },
            { max: 50, message: '最大长度为50个字符', trigger: 'blur' }
          ],
          dimensionCode: [
            { required: true, message: '维度编码不能为空', trigger: 'blur' },
            { max: 50, message: '最大长度为25个字符', trigger: 'blur' }
          ],
          comments: [
            { max: 500, message: '最大长度为500个字符', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询【请填写功能名称】列表 */
      getList() {
        this.loading = true
        listDimension(this.queryParams).then(response => {
          console.log(response.data)
          this.dimensionList = this.handleTree(response.data.records, 'id', 'upperId');
          // resolve = response.data.records;
          // this.total = response.data.total
          this.loading = false
        })
      },
      // 维度名称字典翻译
      dimensionNameFormat(row, column) {
      },
      // 维度类型：1,公用;2,私有；字典翻译
      dimensionTypeFormat(row, column) {
        return this.selectDictLabel(this.dimensionTypeOptions, row.dimensionType)
      },
      // 维度属性：1,动态;2.静态字典翻译
      dimensionAttrFormat(row, column) {
        return this.selectDictLabel(this.dimensionAttrOptions, row.dimensionAttr)
      },
      // 状态：0删除 1启用 2禁用字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status)
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          dimensionName: undefined,
          dimensionCode: undefined,
          upperId: 0,
          dimensionType: '1',
          dimensionAttr: '1',
          status: '1',
          updateDate: undefined,
          comment: undefined,
          activeYear: new Date().getFullYear() + '',
          updateBy: Cookies.get("username")
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        let likes = []
        if(this.queryParams.dimensionName)
          likes.push('dimensionName')
        if(this.queryParams.dimensionCode)
          likes.push('dimensionCode')

        this.queryParams.likes = likes.join(',')
        this.getList()

      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAddBtn(){
        this.handleAdd()
      },
      handleAdd(row) {
        this.reset()
        if (row) {
          this.form.upperId = row.id
        }else{
          this.form.upperId = 0
        }
        this.open = true
        this.title = '添加维度'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const id = row.id || this.ids
        getDimension(id).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改维度'
        })
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateDimension(this.form).then(response => {
                if (response.code === 0) {
                  this.msgSuccess('修改成功')
                  this.open = false
                  this.getList()
                }
              })
            } else {
              addDimension(this.form).then(response => {
                if (response.code === 0) {
                  this.msgSuccess('新增成功')
                  this.open = false
                  this.getList()
                }
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        if( row.children ){
          this.msgError("请先删除子维度");
          return ;
        }
        this.$confirm('是否确认删除"' + row.dimensionName + '"?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delDimension(ids)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        }).catch(function() {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$confirm('是否确认导出所有维度数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return exportDimension(queryParams)
        }).then(response => {
          this.download(response.msg)
        }).catch(function() {
        })
      }
    }
  }
</script>
